package test;


import com.github.abel533.database.Dialect;
import com.github.abel533.database.IntrospectedColumn;
import com.github.abel533.database.IntrospectedTable;
import com.github.abel533.database.SimpleDataSource;
import com.github.abel533.utils.DBMetadataUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuzh
 */
public class test1 {

    public static void main(String[] args) {
       SimpleDataSource dataSource = new SimpleDataSource(
      /* Dialect.MYSQL,
         "jdbc:mysql://localhost:3306/test",
           "root",
           "123"   */
        	 Dialect.ORACLE,
                "jdbc:oracle:thin:@121.248.200.5:1521:orcl",
                "swjsysq",
                "swjsysq" 
 //   		   Dialect.ORACLE,
//               "jdbc:oracle:thin:@10.32.1.6:1521/jssworcl",
//              "r_jssq",
//              "r_jssq"
        );
       SimpleDataSource dataSource2 = new SimpleDataSource(
    	    /*   Dialect.MYSQL,
    	          "jdbc:mysql://localhost:3306/test",
    	           "root",
    	           "123"  */
    	        		 Dialect.ORACLE, 
    	                "jdbc:oracle:thin:@121.248.200.5:1521:orcl",
    	                "SLMETADATA",
    	                "SLMETADATA" 
//    	                Dialect.ORACLE, 
//    	                "jdbc:oracle:thin:@10.32.25.21:1521/jssworcl",
//    	                "SLMETADATA",
//    	                "SLMETADATA"
    	        );
        String str="swjsysq";
       
        DBMetadataUtils dbMetadataUtils = null;
        DBMetadataUtils dbMetadataUtils2 = null;
      

        try {
            dbMetadataUtils = new DBMetadataUtils(dataSource);
            System.out.println("数据库1连接成功");
            dbMetadataUtils2 = new DBMetadataUtils(dataSource2);
            Connection conn=dbMetadataUtils2.getConnection();
            
            System.out.println("数据库2连接成功");
           
          //  PreparedStatement sm =null;
            String mdcode=dbMetadataUtils.getIntrospector().getmdCode(conn);
            int  dscode1=dbMetadataUtils.getIntrospector().getdsCode1(conn);
            int  dscode2=dbMetadataUtils.getIntrospector().getdsCode2(conn);
            int  dscode3=dbMetadataUtils.getIntrospector().getdsCode3(conn);
           
      /*      sm=conn.prepareStatement("select mdcode from SLMETADATA.MD_METADATA");
            ResultSet rs =sm.executeQuery();
            while (rs.next()) {
                mdcode= rs.getString(1);
                }
                rs.close(); */
            InsertBatch insert = new InsertBatch();
            ArrayList<Object []> resultlist1 = new ArrayList<Object []>();
            ArrayList<Object []> resultlist2 = new ArrayList<Object []>();
            ArrayList<Object []> resultlist3 = new ArrayList<Object []>();
            String sql1 = "INSERT INTO MD_LIBRARYDSID(mdCode,dsCode,builddate,charset,version,dsname,location)  VALUES(?,?,?,?,?,?,?)";
            String sql2 = "INSERT INTO MD_CLASSDSID(mdCode,supDscode,dsCode,dsname,beizhu,builddate,starttime)  VALUES(?,?,?,?,?,?,?)";
            String sql3 = "INSERT INTO MD_OBJDSID(mdCode,supDsCode,dsCode,dsName,beizhu,builddate,dstype)  VALUES(?,?,?,?,?,?,?)";
            
            
            Object[] obj1=new Object[7];
            Object[] obj2=new Object[7]; 
            Object[] obj3=new Object[7]; 
           // obj1[0]=null;
            obj1[0]=dbMetadataUtils.getIntrospector().getmdCode(conn);
            obj1[1]=dscode1+1;
            obj1[2]=dbMetadataUtils.getIntrospector().getDatabaseCreatedTime(dbMetadataUtils.getDefaultConfig(),str);
            obj1[3]=dbMetadataUtils.getIntrospector().getCharset(dbMetadataUtils.getDefaultConfig());
            obj1[4]=dbMetadataUtils.getDatabaseMetaData().getDatabaseProductVersion();
           // obj1[5]=dbMetadataUtils.getDefaultConfig().getSchemaPattern();
            obj1[5]=str.toUpperCase();
            obj1[6]=dbMetadataUtils.getDatabaseMetaData().getURL();
            resultlist1.add(obj1);
           
        
            for (Object[] objects : resultlist1) {
    			
    			System.out.println();
    			for (int i = 0; i < 7; i++) {
    				System.out.print(objects[i]+"\t");
    				
    			}
    			 System.out.println(" ");}
      //     insert.saveResults(resultlist1, sql1,conn);
           
           
           
        /*   Statement   statement =dbMetadataUtils.getConnection().createStatement();
           String sql="SELECT CREATED FROM USER_OBJECTS WHERE OBJECT_NAME='FX_APP_VERSION'";
           String sql1="SELECT * FROM USER_TABLES";
          
           ResultSet rs=statement.executeQuery(sql1);
           int col = rs.getMetaData().getColumnCount();   
           while(rs.next()){
        	   for (int i = 1; i <= col; i++) {
                   System.out.print(rs.getString(i) + "\t");
                 //  if ((i == 2) && (rs.getString(i).length() < 8)) {
                      // System.out.print("\t");
                 //  }
                }
               System.out.println("-------");
           }
           rs.close();*/
      //      System.out.println("数据库catlogs："+ dbMetadataUtils.getCatalogs());
      //     System.out.println("数据库schema："+dbMetadataUtils.getSchemas());
      //      System.out.println("数据库schematerm："+dbMetadataUtils.getDatabaseMetaData().getSchemaTerm());
       //     System.out.println("数据库username："+dbMetadataUtils.getDatabaseMetaData().getUserName());
       //     System.out.println("数据库URL: " + dbMetadataUtils.getDatabaseMetaData().getURL());    
      //      System.out.println("是否允许只读:" + dbMetadataUtils.getDatabaseMetaData().isReadOnly());    
     //       System.out.println("数据库的产品名称:" + dbMetadataUtils.getDatabaseMetaData().getDatabaseProductName());    
      //      System.out.println("数据库的版本:" + dbMetadataUtils.getDatabaseMetaData().getDatabaseProductVersion());
       //     System.out.println("数据库的schemapattern:" + dbMetadataUtils.getDefaultConfig().getSchemaPattern());
       //     System.out.println("数据库建立时间:" + dbMetadataUtils.getIntrospector().getDatabaseCreatedTime(dbMetadataUtils.getDefaultConfig()));
       //     System.out.println("数据库的字符集:" + dbMetadataUtils.getIntrospector().getCharset(dbMetadataUtils.getDefaultConfig()));
           
            
         
            
        /*    System.out.println("数据库中使用的表类型");    
            ResultSet rs = dbMetadataUtils.getDatabaseMetaData().getClientInfoProperties();    
            while (rs.next()) {    
                System.out.println(rs.getString(1));    
            }    
            rs.close();     */
           
           List<IntrospectedTable> list = dbMetadataUtils.introspectTables(dbMetadataUtils.getDefaultConfig(),str);
//         
//               for (IntrospectedTable base : list) {
//                System.out.println(base.getRemarks() + ":");   } 
//               System.out.println("---------------------------");
//            for (IntrospectedTable base : list) {
//                System.out.println(base.getCatalog() + ":");   } 
//            System.out.println("---------------------------");
//            for (IntrospectedTable base : list) {
//                System.out.println(base.getName() + ":");   } 
//            System.out.println("---------------------------");
//            
           
           
          
           for (IntrospectedTable table : list) {
        	 
        	   obj2[0]=mdcode;
               obj2[1]=dscode1+1;
               obj2[2]=++dscode2;
               obj2[3]=table.getName();
               obj2[4]=table.getRemarks();
               obj2[5]=dbMetadataUtils.getIntrospector().getTableCreatedTime(dbMetadataUtils.getDefaultConfig(),str,table.getName());
               obj2[6]=null;
               resultlist2.add(obj2);
               for (Object[] objects2 : resultlist2) {
       			
       			System.out.println();
       			for (int i = 0; i < 7; i++) {
       				System.out.print(objects2[i]+"\t");
       				
       			}
       		     System.out.println(" ");}
        //     insert.saveResults(resultlist2, sql2,conn);
           
               resultlist2.clear();
         //    System.out.println(table.getName() + ":");
        //        System.out.println(table.getRemarks() + ":");
       //         System.out.println("表建立时间:" + dbMetadataUtils.getIntrospector().getTableCreatedTime(dbMetadataUtils.getDefaultConfig(),table.getName()));
       //         System.out.println("表DML时间:" + dbMetadataUtils.getIntrospector().getTableDMLTime(dbMetadataUtils.getDefaultConfig(),table.getName()));
      //          System.out.println("表Rows:" + dbMetadataUtils.getIntrospector().getRowNum(dbMetadataUtils.getDefaultConfig(),table.getName()));
      //          System.out.println("表AnalyzedTime:" + dbMetadataUtils.getIntrospector().getAnalyzedTime(dbMetadataUtils.getDefaultConfig(),table.getName()));
                for (IntrospectedColumn column : table.getAllColumns()) {
                	 
                	obj3[0]=mdcode;
                    obj3[1]=dscode2;
                    obj3[2]=++dscode3;
                    obj3[3]=column.getName();
                    obj3[4]=column.getRemarks();
                    obj3[5]=dbMetadataUtils.getIntrospector().getTableCreatedTime(dbMetadataUtils.getDefaultConfig(),str,table.getName());
                    obj3[6]=null;
                    resultlist3.add(obj3);
                    for (Object[] objects3 : resultlist3) {
            			
            			System.out.println();
            			for (int i = 0; i < 7; i++) {
            				System.out.print(objects3[i]+"\t");
            				
            			}
            			
            			 System.out.println(" ");}
                  
           //      insert.saveResults(resultlist3, sql3,conn);
                    resultlist3.clear();
                   
                  
          /*          System.out.println(column.getName() + " -pk- " +
                    		column.isPk() + " - " +
                            column.getJdbcTypeName() + " ( " +
                            column.getLength() + " ) " + " -nullable-  " +
                           
                            
                            column.isNullable() + " - " + " -dftvlue- " +
                           
                            column.getDefaultValue()+ " - " +
                            column.getRemarks()+ " -scale-  "+
                           
                            column.getScale()); */
                }
            }  
          
           
           conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }   

}

